package com.haochang.chunkvod.controller.fragment.hostess;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseEditText;
import com.haochang.base.BaseFragment;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.view.vod.dialog.KeyBoardDialog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

/**
 *         created at 2016/8/25 11:02
 *         房间设置页面
 */
public class RoomSettingFragment extends BaseFragment implements KeyBoardDialog.KeyBoardOnSureListener
{
	private BaseEditText et_count, et_desc;

	private String billId;
	private String remarks = "";
	private String comstmerCount;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_room_setting, container,false);
		initView(view);
		return view;
	}

	private void initView(View view)
	{
		RelativeLayout group_count = (RelativeLayout) view.findViewById(R.id.group_count);//人数
		Button room_setting_ok = (Button) view.findViewById(R.id.room_setting_ok);//提交按钮
		et_count = (BaseEditText) view.findViewById(R.id.et_count);
		et_desc = (BaseEditText) view.findViewById(R.id.et_desc);
		group_count.setOnClickListener(baseClickListener);
		et_count.setOnClickListener(baseClickListener);
		et_count.addTextChangedListener(new TextWatcher()
		{
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after)
			{

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
				String countStr = et_count.getText().toString().trim();
				if (!TextUtils.isEmpty(countStr) && Integer.parseInt(countStr) > 1000)
				{
					ToastUtils.showText(getActivity(), "房间人数超过1000");
					et_count.setText("");
				}
			}

			@Override
			public void afterTextChanged(Editable s)
			{

			}
		});
		room_setting_ok.setOnClickListener(baseClickListener);
		billId = getArguments().getString(IntentKey.BILL_ID);
		comstmerCount = getArguments().getString(IntentKey.COMSTMER_COUNT);
		remarks = getArguments().getString(IntentKey.REMARKS);
		if (!TextUtils.isEmpty(comstmerCount) && Integer.parseInt(comstmerCount) > 0)
		{
			et_count.setText(comstmerCount);
		}
		if (!TextUtils.isEmpty(remarks))
		{
			et_desc.setText(remarks);
			et_desc.setSelection(et_desc.getText().toString().length());
		}
	}

	private OnBaseClickListener baseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.room_setting_ok:
				final String count = et_count.getText().toString().trim();
				if (TextUtils.isEmpty(count) || Integer.parseInt(count) <= 0)
				{
					ToastUtils.showText(getActivity(), "请输入人数");
					return;
				}
				final String remark = et_desc.getText().toString().trim();
				if(TextUtils.equals(count,comstmerCount)&&TextUtils.equals(remark,remarks)){
					ToastUtils.showText(getActivity(),"没有修改数据，无需提交!");
					return;
				}
				if (!TextUtils.isEmpty(billId))
				{
					new HostessData((BaseActivity) getActivity()).requestUpdateBill(billId, count, remark, new HostessData.NoticeListener()
					{
						@Override
						public void onRequestSuccess(String notice)
						{
							if (!TextUtils.isEmpty(notice))
							{
								Intent intent = getActivity().getIntent();
								intent.putExtra(IntentKey.ROOM_ID, getArguments().getString(IntentKey.ROOM_ID));
								intent.putExtra(IntentKey.REMARKS, remark);
								intent.putExtra(IntentKey.COMSTMER_COUNT, count);
								getActivity().setResult(Activity.RESULT_OK, intent);//先返回数据，本地先刷新，再网络刷新
								ToastUtils.showText(getActivity(), "修改成功");
								getActivity().finish();
							}
						}

						@Override
						public void onFail(int errno, String errMsg)
						{

						}
					});
				}
				break;
			case R.id.et_count:
			case R.id.group_count:
				KeyBoardDialog keyBoardDialog = new KeyBoardDialog(getActivity());
				keyBoardDialog.setKeyBoardOnSureListener(RoomSettingFragment.this);
				keyBoardDialog.show();
				break;
			}
		}
	};

	@Override
	public void setValue(String value)
	{
		try
		{
			if (value.contains("."))
				ToastUtils.showText(getActivity(), R.string.error_number);
			else if (Integer.parseInt(value) <= 0)
				ToastUtils.showText(getActivity(), R.string.error_number);
			else
			{
				et_count.setText(value);
			}

		} catch (

		Exception e)
		{
			ToastUtils.showText(getActivity(), getResources().getString(R.string.error_max_number));
		}
	}
}
